package cn.lili.controller.wallet;

import cn.lili.common.enums.ResultUtil;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.wallet.entity.dto.MemberWalletDTO;
import cn.lili.modules.wallet.entity.vo.MemberWalletSearchParams;
import cn.lili.modules.wallet.service.MemberWalletService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * create by yudan on 2022/3/27
 */
@RestController
@Api(tags = "商家端,预存款接口")
@RequestMapping("/store/members/wallet")
public class MemberWalletSellerController {
    @Autowired
    private MemberWalletService memberWalletService;

    @PostMapping(value = "/allStore")
    @ApiOperation(value = "商家钱包收入汇总")
    public ResultMessage<Map<String ,Object>> allStore(MemberWalletSearchParams memberWalletSearchParams) {
        Map<String ,Object> map = new HashMap<>();
        //支出
        IPage<MemberWalletDTO> memberWallet = memberWalletService.findCommissionVOStore(memberWalletSearchParams);
        Double outMoney = 0D;
        if(CollectionUtils.isNotEmpty(memberWallet.getRecords())){
            for (MemberWalletDTO c : memberWallet.getRecords()) {
                outMoney = CurrencyUtil.add(c.getMoney() != null ? c.getMoney() : 0D ,outMoney);
            }
        }
        map.put("outMoney",outMoney);
        //收入
        IPage<MemberWalletDTO> memberWalletDTO = memberWalletService.findMemberWalletDTOStore(memberWalletSearchParams);
        Double inMoney = 0D;
        if(CollectionUtils.isNotEmpty(memberWalletDTO.getRecords())){
            for (MemberWalletDTO me : memberWalletDTO.getRecords()) {
                inMoney = CurrencyUtil.add(me.getMoney() != null ? me.getMoney() : 0D,inMoney);
            }
        }
        map.put("inMoney",inMoney);
        map.put("allMoney",CurrencyUtil.sub(inMoney,outMoney));
        return ResultUtil.data(map);
    }


    @PostMapping(value = "/outlayStore")
    @ApiOperation(value = "商家钱包支出明细")
    public ResultMessage<IPage<MemberWalletDTO>> outlayStore(MemberWalletSearchParams memberWalletSearchParams) {
        return ResultUtil.data(memberWalletService.findCommissionVOStore(memberWalletSearchParams));
    }

    @PostMapping(value = "/incomeStore")
    @ApiOperation(value = "商家钱包收入明细")
    public ResultMessage<IPage<MemberWalletDTO>> incomeStore(MemberWalletSearchParams memberWalletSearchParams) {
        return ResultUtil.data(memberWalletService.findMemberWalletDTOStore(memberWalletSearchParams));
    }

}
